package com.quan.cms.rpc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quan.base.common.base.service.support.BaseService;
import com.quan.base.common.exception.BizException;
import com.quan.base.common.utils.Paginator;
import com.quan.cms.rpc.api.CmsContentExtService;
import com.quan.cms.rpc.api.CmsContentService;
import com.quan.cms.rpc.api.CmsContentTxtService;
import com.quan.cms.rpc.dao.mapper.CmsContentMapper;
import com.quan.cms.rpc.extend.ContentQuery;
import com.quan.cms.rpc.extend.ContentWrapper;
import com.quan.cms.rpc.model.CmsContent;
import com.quan.cms.rpc.model.CmsContentExt;
import com.quan.cms.rpc.model.CmsContentTxt;

@Service
public class CmsContentServiceImpl extends BaseService<CmsContent, Integer> implements CmsContentService {
	
	@Autowired
	private CmsContentMapper mapper ;
	
	@Autowired
	private CmsContentExtService contentExtService ;

	@Autowired
	private CmsContentTxtService contentTxtService ;
	
	@Transactional
	@Override
	public void saveContentWrapper(ContentWrapper contentWrapper) {
		//内容
		CmsContent content = super.insertSelective(contentWrapper.getContent());
		
		//内容扩宽
		CmsContentExt contentExt = contentWrapper.getContentExt();
		contentExt.setContentId(content.getId());
		this.contentExtService.insertSelective(contentExt);
		
		//内容文本
		CmsContentTxt contentTxt = contentWrapper.getContentTxt() ;
		contentTxt.setContentId(content.getId());
		this.contentTxtService.insertSelective(contentTxt) ;
	}

	@Override
	public PageInfo<CmsContent> selectPageContentWrapper(Paginator paginator, ContentQuery query) {
		try {
			Page<CmsContent> page = PageHelper.startPage(paginator.getPage(), paginator.getRows()) ;
			this.mapper.selectPageContentWrapper(paginator, query);
			return new PageInfo<CmsContent>(page) ;
		} catch (Exception e) {
			throw BizException.DB_LIST_EXCEPTION.errorMsg(e) ;
		}
	}

}
